package com.ht.module.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ht.abnormal.HtException;
import com.ht.module.bus.entity.*;
import com.ht.module.bus.service.*;
import com.ht.module.common.service.CommonService;

import com.ht.util.CommMethod;
import com.ht.util.DateUtil;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ProjectName: ht
 * @ClassName: CommonServiceImpl
 * @Author: hejialun
 * @Description: 公共类实现类
 * @Date: 2021/9/14 16:14
 */
@Service
public class CommonServiceImpl implements CommonService {
    @Value("${upload.path}")
    private String tempPath;
    @Value("${upload.prefix}")
    private String prefix;
    @Resource
    private TDistrictService districtService;
    @Resource
    private TStudentService studentService;
    @Resource
    private TSchoolService schoolService;
    @Resource
    private TRouteService routeService;
    @Resource
    private TVehicleInformationService vehicleInformationService;
    @Resource
    private TTransportationCompanyInformationService tTransportationCompanyInformationService;

    @Override
    public String uploadFile(MultipartFile file) {
        //判断文件是否存在
        if(null==file){
            throw new HtException("文件不存在!");
        }
        //获取文件格式
        //获取文件的后缀名 .jpg
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);

        //创建文件猴嘴
        if (!new File(tempPath+prefix+suffix).exists()) {
            new File(tempPath+prefix+suffix).mkdirs();
        }
        String date= DateUtil.getDate();

        //判断当前文件夹是否存在
        if (!new File(tempPath+prefix+suffix+"/"+date).exists()) {
            new File(tempPath+prefix+suffix+"/"+date).mkdirs();
        }
        //重命名文件
        String filePath=prefix+suffix+"/"+date+"/"+ UUID.randomUUID().toString()+"_"+file.getOriginalFilename();

        try{
            file.transferTo(new File(tempPath+filePath));
        }catch (Exception e){
            throw new HtException(e.getMessage());
        }
        return  filePath;
    }

    @Override
    public HashMap<String, Integer> syNumberStatistics() {
        HashMap<String, Integer> map = new HashMap<>();

        //覆盖学校
        map.put("fgxx",schoolService.list(
                new QueryWrapper<TSchool>()
        ).size());

        //覆盖乡镇
        map.put("fgxz",districtService.list(
                new QueryWrapper<TDistrict>()
        ).size());

        //服务学生
        map.put("fwxs",studentService.list(
                new QueryWrapper<TStudent>()
        ).size());

        //开通路线
        map.put("ktlx",routeService.list(
                new QueryWrapper<TRoute>()
        ).size());

        //承运车辆
        map.put("cycl",vehicleInformationService.list(
                new QueryWrapper<TVehicleInformation>()
        ).size());


        //参与企业
        map.put("cyqy",tTransportationCompanyInformationService.list(
                new QueryWrapper<TTransportationCompanyInformation>()
        ).size());
        return map;
    }
}